﻿using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using HomagChina.CCT.BaseUI.DataModel;
using System;
using System.Windows;

namespace HomagChina.CCT.BaseUI
{
    public class HomePlateDialogViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        //private SearchInfoHandle Handle_Search;
        //private InsertInfoHandle Handle_Insert;
        //private ModifyInfoHandle Handle_modify;
        //private CommonHandle commonHandle;

        #endregion Handle

        #region 属性

        private string _OrderId;
        private float _Length;
        private float _Width;
        private float _Thickness;
        private string _Reference;
        private int _OrderPackageCount;
        private string _EditVisible;
        private string _SaveVisible;
        private bool _IsReadOnly;

        public float Length
        {
            get => _Length;
            set { _Length = value; OnPropertyChanged(); }
        }

        public float Width
        {
            get => _Width;
            set { _Width = value; OnPropertyChanged(); }
        }

        public float Thickness
        {
            get => _Thickness;
            set { _Thickness = value; OnPropertyChanged(); }
        }

        public string OrderId
        {
            get => _OrderId;
            set { _OrderId = value; OnPropertyChanged(); }
        }

        public string Reference
        {
            get => _Reference;
            set { _Reference = value; OnPropertyChanged(); }
        }

        public int OrderPackageCount
        {
            get => _OrderPackageCount;
            set { _OrderPackageCount = value; OnPropertyChanged(); }
        }

        public string EditVisible
        {
            get => _EditVisible;
            set { _EditVisible = value; OnPropertyChanged(); }
        }

        public string SaveVisible
        {
            get => _SaveVisible;
            set { _SaveVisible = value; OnPropertyChanged(); }
        }

        public bool IsReadOnly
        {
            get => _IsReadOnly;
            set { _IsReadOnly = value; OnPropertyChanged(); }
        }

        private object _SelectedValue = "10";

        public object SelectedValue
        {
            get => _SelectedValue;
            set { _SelectedValue = value; OnPropertyChanged(); }
        }

        //提交
        public RelayCommand ApplyCommand { get; }

        public RelayCommand ModifyCommand { get; }

        //页面加载
        public RelayCommand LoadedCommand { get; }

        #endregion 属性

        #region components

        public DropDownList DropDownList_Location { get; set; }

        public InputField _InputField { get; set; }

        public InputField InputField
        {
            get => _InputField;
            set { _InputField = value; }
        }

        //private T_TransOnline_Infeed T_Online;

        #endregion components

        //构造函数
        public HomePlateDialogViewModel(BoardInfoModel ModifyboardInfoModel, Boolean IsEdit)
        {
            if (ModifyboardInfoModel != null)
            {
                this.Length = (float)ModifyboardInfoModel.Length;
                this.Width = (float)ModifyboardInfoModel.Width;
                this.Thickness = (float)ModifyboardInfoModel.Thickness;
                SelectedValue = ModifyboardInfoModel.Location;
                this.Reference = ModifyboardInfoModel.Reference;
            }
            this.EditVisible = IsEdit ? "Visible" : "Collapsed";
            this.SaveVisible = IsEdit ? "Collapsed" : "Visible";
            this.IsReadOnly = !IsEdit;
            ApplyCommand = new RelayCommand(Apply);
            ModifyCommand = new RelayCommand(Modify);
            LoadedCommand = new RelayCommand(ViewLoadedCommand);
            //Handle_Search = new SearchInfoHandle();
            //Handle_Insert = new InsertInfoHandle();
            //Handle_modify = new ModifyInfoHandle();
            //commonHandle = new CommonHandle();
            LoadDropDownList();
        }

        public void ViewLoadedCommand(object InputFieldInfo)
        {
            InputField = (InputField)InputFieldInfo;
            InputField.LostFocus += InputField_LostFocus;
        }

        private void InputField_LostFocus(object sender, RoutedEventArgs e)
        {
            //查询数据库内是否存在该板件
            //var PartInfo = Handle_Search.GetPartInfoByReference(InputField.Text);

            //if (PartInfo == null)
            //{
            //    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.The_board_information_does_not_exist_in_the_current_database__please_confirm_and_enter_);
            //    InputField.Clear();
            //    return;
            //}
            //Length = (float)PartInfo.Length;
            //Width = (float)PartInfo.Width;
            //Thickness = (float)PartInfo.Thickness;
        }

        #region DropDownList

        /// <summary>
        /// 加载DropDownList内容
        /// Load the DropDownList
        /// </summary>
        public void LoadDropDownList()
        {
            DropDownList_Location = new DropDownList();
            //DropDownList_Location.ItemsSource = (Handle_Search.GetLocationInfo(true));
        }

        #endregion DropDownList

        //提交函数
        private void Apply()
        {
            //T_Online = new T_TransOnline_Infeed()
            //{
            //    Reference = InputField.Text,
            //    Location = SelectedValue.ToString(),
            //    State = 0,
            //    Timestamp = DateTime.Now,
            //};

            //写入
            //if (Handle_Insert.InsertOnlinePart(T_Online))
            //{
            //    commonHandle.CloseDiaLog("HomeAddPlateDialog");
            //    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Congratulations__information_added_successfully);
            //}
            //else
            //{
            //    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Sorry__information_addition_failed__please_try_again_or_contact_administrator);
            //    //HomagGroup.Base.UI.Windows.MessageBox.Show(localText.);
            //}
        }

        private void Modify()
        {
            ////编辑
            //if (Handle_modify.ModifyBoardInfo(Width, Length, Thickness, SelectedValue.ToString(), Reference))
            //{
            //    commonHandle.CloseDiaLog("HomeAddPlateDialog");
            //    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Congratulations__information_added_successfully);
            //}
            //else
            //{
            //    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Sorry__information_addition_failed__please_try_again_or_contact_administrator);
            //}
        }
    }
}